﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Tesla.Content;
using Tesla.Graphics;

namespace Tesla.Xna.Content.Loaders {
    public sealed class XNAEffectLoader : ResourceLoader<Effect> {
        private TeslaProcessorContext _xnaContext;
        private EffectProcessor _processor;

        /// <summary>
        /// Gets or sets the XNA Pipeline context. This should be set if the target platform/graphics profile
        /// should be different from Windows platform or the HiDef profile.
        /// </summary>
        public TeslaProcessorContext XNAContext {
            get {
                return _xnaContext;
            }
            set {
                if(value == null) {
                    throw new ArgumentNullException("Context may not be null");
                }
                _xnaContext = value;
            }
        }

       public XNAEffectLoader() : base(".fx") {
           _xnaContext = new TeslaProcessorContext();
           _processor = new EffectProcessor();
       }

        public override Object Load(IResource resource, Type type, LoaderParameters parameters) {
            if(type.Equals(this.TargetType)) {
                return Load(resource, parameters);
            } else {
                throw new InvalidCastException("Invalid type to load, must be an Effect.");
            }
        }

        public override Effect Load(IResource resource, LoaderParameters parameters) {
            FileInfo info = new FileInfo(resource.FullName);
            if(!info.Exists) {
                throw new FileNotFoundException("Effect file does not exist in directory");
            }
            EffectContent content = new EffectContent();
            content.Identity = new ContentIdentity(resource.FullName);
            content.EffectCode = File.ReadAllText(resource.FullName);

            CompiledEffectContent compiled = _processor.Process(content, _xnaContext);

            Effect effect = new Effect(compiled.GetEffectCode());
            effect.Name = resource.Name;
            return effect;
        }
    }
}
